import axios from 'axios';
import { ElMessage } from 'element-plus';

let baseURL = '/api';

if (process.env.NODE_ENV === 'production') {
  baseURL = '';
}

const request = axios.create({
  baseURL
});

request.interceptors.request.use((config: IObj) => {
  let token = window.sessionStorage.getItem('token');

  // if (token) {
  //   token = token.substring(7);
  // }
  config.headers.Authorization = token;

  return config;
})

request.interceptors.response.use((res: IObj) => {

  if (res.data.code && res.data.code !== 200) {
    const msg = res.data.msg ? res.data.msg : res.data.message;
    
    ElMessage({
      message: msg,
      type: 'error'
    })
  }

  return res;
})


export default request;

export { baseURL };